package com.bigflexlib.x.flash.system.gc
{


	import flash.system.System;
	import flash.utils.Dictionary;


	
	
	public class GCUtil
	{
		

		/**
		 * 
		 * @param map
		 * @return An array containing two elements: the number of values before(0) and after(1) gc.
		 * 
		 */	
		public static function gcMapValues( map:* ):Array
		{
	
			var 	key		:*, 
					i		:int			= 0,
					report	:Array			= [ ],
					dict	:Dictionary 	= new Dictionary( true )
					;
			
			// 1) copy all elements in the cache to a weak-key inverted dictionary			
			for ( key in map )
			{
				i++;
				dict[ map[ key ] ] = key;
				delete map[ key ];
			}
		
			report.push( i );
		

			// 2) run GC
			System.gc( );
			System.gc( );
			
			

			// 3) restore map with surviving elements
			i = 0;	

			for ( key in dict )
			{
				i++;
				map[ dict[ key ] ] = key;
			}

			report.push( i );
			
			return report;				

		}


	}
	
}